package com.heliang.ui.info;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalBitmap;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.example.appxiehui2.R;
import com.heliang.common.ImageDispose;
import com.heliang.dataaccess.bean.AdvBean;
import com.heliang.util.Constants;
import com.heliang.util.ImageLoaderAddLoadingUtil;
import com.hl.component.LoadindImageView;
import com.hl.component.LoadingView;

/**
 * @ClassName: AdvAdapter.java
 * @Description: 四屏滑动广告适配器
 * @author heliang
 * @version V1.0
 * @Date 2013-10-16 下午9:21:28
 */
public class AdvAdapter extends PagerAdapter {
    private Context context;

    private List<AdvBean> advList = new ArrayList<AdvBean>();

    private Bitmap bitMap;

    public AdvAdapter(Context context, List<AdvBean> data) {
        this.context = context;
        this.advList = data;
    }

    @Override
    public int getCount() {
        return advList.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public Object instantiateItem(View container, int position) {   
        ImageView imageView = new ImageView(context);
        
        AdvBean advBean = advList.get(position);
        
        /* 将实际图转化为bitMap */
        bitMap = ImageDispose.drawableToBitmap(context.getResources().getDrawable(R.drawable.banner_default));
        bitMap = ImageDispose.zoomBitmap(bitMap, Constants.SCREEN_WIDTH, Constants.SCREEN_WIDTH * bitMap.getHeight() / bitMap.getWidth());

        LoadingView v1 = new LoadindImageView(context, bitMap, false);
        
        /* 固定大小的大图片使用该种方式下载图片到sd卡  */
        v1 = ImageLoaderAddLoadingUtil.instance.setImageDrawableZoom(advBean.getImgPath(), advBean.getImgUrl(), v1);
        imageView.setScaleType(ScaleType.CENTER_CROP);
        ((ViewPager) container).addView(v1);
        return v1;
    }
}
